package com.asset.admin.controller;

import com.asset.admin.system.syemenu.form.MenuForm;
import com.asset.admin.system.tradingtype.biz.ITradingTypeBiz;
import com.asset.admin.dao.entity.TradingTypeEntity;
import com.asset.admin.system.tradingtype.form.TradingTypeForm;
import com.asset.admin.system.tradingtype.form.TradingTypeQueryForm;
import com.asset.common.beans.IdParam;
import com.asset.common.pageform.PageForm;
import com.asset.user.dao.entity.UserEntity;
import com.asset.user.sevice.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * <p>
 * 交易类型controller
 * </p>
 *
 * @author 牧之
 * @date 2024/2/7 16:07
 */
@RestController
@RequestMapping("/trading")
@Slf4j
public class TradingTypeController {

    @Resource
    private ITradingTypeBiz tradingTypeBiz;

    @Resource
    private IUserService userService;

    @ResponseBody
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public void create(@RequestBody TradingTypeForm form){
        tradingTypeBiz.create(form);
    }

    // TODO 不能物理删除  需要逻辑删除  不然兼容不了老数据
    @ResponseBody
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public void deleteById(@RequestBody IdParam idParam){
        tradingTypeBiz.deleteById(idParam.getId());
    }

    @ResponseBody
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public void update(@RequestBody TradingTypeForm updateForm){
        tradingTypeBiz.update(TradingTypeForm.convertForm2Entity(updateForm));
    }

    @ResponseBody
    @RequestMapping(value = "/getById", method = RequestMethod.POST)
    public TradingTypeForm getById(@RequestBody IdParam idParam){
        return tradingTypeBiz.getById(idParam.getId());
    }

    @ResponseBody
    @RequestMapping(value = "/getListByQuery", method = RequestMethod.GET)
    public PageForm<TradingTypeForm> getListByQuery(TradingTypeQueryForm queryForm){
        PageForm<TradingTypeForm> pageForm = new PageForm<>();
        pageForm.setPageNum(queryForm.getPageNum());
        pageForm.setPageSize(queryForm.getPageSize());
        List<TradingTypeEntity> entities = tradingTypeBiz.getListByQuery(queryForm);
        List<Long> userIds = entities.stream().map(TradingTypeEntity::getCreator).collect(Collectors.toList());
        List<UserEntity> userEntities = userService.getUsersByIds(userIds);
        Map<Long, String> userIdNameMap = userEntities.stream().collect(Collectors.toMap(UserEntity::getId, UserEntity::getName));
        int total = tradingTypeBiz.getCount(queryForm);
        List<TradingTypeForm> typeForms = entities.stream()
                .map(tradingTypeEntity -> TradingTypeForm.convertEntity2Form(tradingTypeEntity, userIdNameMap))
                .collect(Collectors.toList());
//        List<TradingTypeForm> typeForms = entities.stream().map(TradingTypeForm::convertEntity2Form).collect(Collectors.toList());
        pageForm.setTotal(total);
        pageForm.setDataList(typeForms);
        return pageForm;
    }
}
